package com.zy.garbage.service.impl;

import com.zy.garbage.entity.Exam;
import com.zy.garbage.entity.User;
import com.zy.garbage.enums.UserStatusEnum;
import com.zy.garbage.exception.CommonException;
import com.zy.garbage.repository.ExamRepository;
import com.zy.garbage.repository.UserRepository;
import com.zy.garbage.service.ExamService;
import com.zy.garbage.util.KeyUtil;
import com.zy.garbage.util.MyDateUtil;
import com.zy.garbage.vo.ExamVO;
import com.zy.garbage.vo.UserExamVO;
import com.zy.garbage.vo.UserSimpleInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamRepository examRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public Integer save(Integer userId) {
        if (!userRepository.findById(userId).isPresent()){
            throw new CommonException(500,"用户不存在");
        }
        User user = userRepository.findById(userId).get();
        if (!UserStatusEnum.ACCEPT.getStatus().equals(user.getUserStatus())){
            throw new CommonException(600,"账号审核未通过");
        }
        Exam exam = new Exam();
        exam.setUserId(userId);
        String examSn = KeyUtil.createUniqueKey();
        while (examRepository.findAllByExamSn(examSn).size()!=0){
            examSn = KeyUtil.createUniqueKey();
        }
        exam.setExamSn(examSn);
        Exam exam1 = examRepository.save(exam);
        if (exam1.getExamId()!=null)
            return exam1.getExamId();
        throw new CommonException(500,"创建问卷失败");
    }

    @Override
    public List<ExamVO> findByUserId(Integer userId) {
        if (examRepository.findAllByUserId(userId).size()==0){
            return null;
        }
        List<ExamVO> examVOList = new ArrayList<>();
        List<Exam> examList = examRepository.findAllByUserId(userId);
        for (Exam exam : examList) {
            ExamVO examVO = new ExamVO();
            BeanUtils.copyProperties(exam,examVO);
            examVO.setCreateTime(MyDateUtil.date2String(exam.getCreateTime()));
            examVOList.add(examVO);
        }
        return examVOList;
    }

    @Override
    public List<ExamVO> findAll() {
        List<Exam> examList = examRepository.findAll();
        List<User> userList = userRepository.findAll();
        HashMap<Integer,User> userMap = new HashMap<>();
        for (User user : userList) {
            userMap.put(user.getUserId(),user);
        }
        List<ExamVO> examVOList = new ArrayList<>();
        for (Exam exam : examList) {
            ExamVO examVO = new ExamVO();
            BeanUtils.copyProperties(exam,examVO);
            Integer userId = exam.getUserId();
            examVO.setUserName(userMap.get(userId).getUserName());
            examVO.setUserPhone(userMap.get(userId).getUserPhone());
            examVO.setCreateTime(MyDateUtil.date2String(exam.getCreateTime()));
            examVOList.add(examVO);
        }
        return examVOList;


    }
}
